import { Injectable } from '@nestjs/common';
import { RoleEntity } from './role.entity';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository, UpdateResult } from 'typeorm';
import { ObjectType } from 'src/types';

@Injectable()
export class RoleService {
  constructor(
    @InjectRepository(RoleEntity)
    private readonly roleRepo: Repository<RoleEntity>,
  ) {}

  async createRole(data: ObjectType): Promise<RoleEntity> {
    const role = await this.roleRepo.create(data);
    return await this.roleRepo.save(role);
  }

  // 逻辑删除
  async deleteRoleById(id: number): Promise<UpdateResult> {
    return await this.roleRepo.update(id, { isDel: 1 });
  }

  // 更新数据
  async modifyRoleById(id: number, data: ObjectType): Promise<UpdateResult> {
    return await this.roleRepo.update(id, data);
  }

  // 用户列表
  async roleList(): Promise<any> {
    // 简单查询
    return await this.roleRepo.find();
  }

  async roleById(id: number): Promise<RoleEntity> {
    return await this.roleRepo.findOne(id);
  }
}
